package com.jlnk.androidfmc.fragment.fragments;

import android.content.Intent;
import android.os.Bundle;

import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jlnk.androidfmc.ChatActivity;
import com.jlnk.androidfmc.MainActivity;
import com.jlnk.androidfmc.R;

public class MessageFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private LinearLayout msgContainer;
    private View textViewConsultation;


    public MessageFragment() {
        // Required empty public constructor
    }
    public static MessageFragment newInstance(String param1, String param2) {
        MessageFragment fragment = new MessageFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_message, container, false);
    }

    public void updateUI() {
        if (MainActivity.consultationInfo != null) {
            updateConsultationInfo("专家姓名", MainActivity.consultationInfo);
            // 清除信息，避免重复显示
            MainActivity.consultationInfo = null;
        }
    }


    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // 首次创建视图时检查更新
        updateUI();
        msgContainer = view.findViewById(R.id.msg_container);
        textViewConsultation = view.findViewById(R.id.consultation_info);
        // 通知MainActivity在非首页隐藏"去卖货"按钮和图片
        MainActivity mainActivity = (MainActivity) getActivity();
        if (mainActivity != null) {
            mainActivity.setGoSellVisibility(false);
        }
        // 设置系统窗口 insets 监听器
        ViewCompat.setOnApplyWindowInsetsListener(view, (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        handleIntentData();

        // 设置消息列表点击事件
        msgContainer.setOnClickListener(v -> {
            Intent intent = new Intent(getContext(), ChatActivity.class);
            startActivity(intent);
        });
    }

    // 可以保留onResume方法来处理用户回到这个Fragment的情况
    @Override
    public void onResume() {
        super.onResume();
        updateUI();
    }

    private void handleIntentData() {
        Intent intent = getActivity().getIntent();
        String expertName = intent.getStringExtra("EXPERT_NAME");
        String consultationContent = intent.getStringExtra("CONSULTATION_CONTENT");

        // 如果有传递的数据，创建一个新的LinearLayout并添加到msgContainer中
        if (expertName != null && consultationContent != null) {
            addMessageItem(expertName, consultationContent);
        }

    }

    private void addMessageItem(String expertName, String consultationContent) {
        LayoutInflater inflater = getLayoutInflater();
        LinearLayout messageItem = (LinearLayout) inflater.inflate(R.layout.item_message, msgContainer, false);

        // 确保item_message布局中的TextView ID与下面调用的ID匹配
        TextView textViewExpertName = messageItem.findViewById(R.id.consultation_title);
        TextView textViewConsultationTime = messageItem.findViewById(R.id.consultation_time);
        TextView textViewConsultationInfo = messageItem.findViewById(R.id.consultation_info);

        if (textViewExpertName != null && textViewConsultationTime != null && textViewConsultationInfo != null) {
            textViewExpertName.setText(expertName);
            textViewConsultationTime.setText("刚刚"); // 这里可以设置为当前时间或其他逻辑
            textViewConsultationInfo.setText(consultationContent);
            msgContainer.addView(messageItem);
        } else {
            // 如果任何一个TextView为null，记录日志或处理错误
            Log.e("MessageFragment", "One of the TextViews is null in item_message layout.");
        }
    }

    // MessageFragment.java
// MessageFragment.java

    public void updateConsultationInfo(String expertName, String consultationContent) {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        LinearLayout messageItem = (LinearLayout) inflater.inflate(R.layout.item_message, msgContainer, false);
        // 确保您正确地设置了文本视图
        TextView textViewExpertName = messageItem.findViewById(R.id.consultation_title);
        TextView textViewConsultationTime = messageItem.findViewById(R.id.consultation_time);
        TextView textViewConsultationInfo = messageItem.findViewById(R.id.consultation_info);
        textViewExpertName.setText(expertName);
        textViewConsultationTime.setText("刚刚"); // 可以设置为当前时间或其他逻辑
        textViewConsultationInfo.setText(consultationContent);
        msgContainer.addView(messageItem);
    }
}